{% extends "base.html" %}

{% block title %}{{ title }} - ExamPulse{% endblock %}

{% block content %}
<div class="container mt-4 mb-6">
    <!-- 页面头部：标题 + 返回按钮 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="text-primary">{{ title }}</h2>
        <a href="{% url 'score:score_detail' pk=exam_record.pk %}" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-1"></i>返回成绩详情
        </a>
    </div>

    <!-- 编辑表单卡片 -->
    <div class="card shadow-sm border-0">
        <div class="card-body p-5">
            <!-- 消息提示（错误/成功） -->
            {% if messages %}
                {% for msg in messages %}
                    <div class="alert alert-{{ msg.tags }} alert-dismissible fade show" role="alert">
                        {{ msg }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                {% endfor %}
            {% endif %}

            <!-- 表单说明：提示关键约束 -->
            <div class="alert alert-info mb-4">
                <i class="fas fa-info-circle me-2"></i>
                <strong>编辑说明</strong>：
                <ul class="mb-0 mt-2 list-disc list-inside">
                    <li>得分不能超过当前科目满分（{{ exam_record.subject.full_score }}分）</li>
                    <li>同一学生的「考试+科目」组合不能重复</li>
                    <li>若该成绩有关联失分明细，修改后总分失分值需匹配</li>
                </ul>
            </div>

            <!-- 渲染编辑表单 -->
            <form method="POST" novalidate>
                {% csrf_token %}

                <!-- 第一行：考试名称 + 考试科目 -->
                <div class="row g-4 mb-4">
                    <div class="col-md-6">
                        <label for="{{ form.exam.id_for_label }}" class="form-label fw-bold">
                            {{ form.exam.label }} <span class="text-danger">*</span>
                        </label>
                        {% if form.exam.errors %}
                            <div class="text-danger mb-1 small">{{ form.exam.errors.0 }}</div>
                        {% endif %}
                        {{ form.exam }}
                    </div>
                    <div class="col-md-6">
                        <label for="{{ form.subject.id_for_label }}" class="form-label fw-bold">
                            {{ form.subject.label }} <span class="text-danger">*</span>
                        </label>
                        {% if form.subject.errors %}
                            <div class="text-danger mb-1 small">{{ form.subject.errors.0 }}</div>
                        {% endif %}
                        {{ form.subject }}
                        <div class="form-text mt-1">当前科目满分：{{ exam_record.subject.full_score }}分</div>
                    </div>
                </div>

                <!-- 第二行：得分 + 考试用时 -->
                <div class="row g-4 mb-4">
                    <div class="col-md-6">
                        <label for="{{ form.score.id_for_label }}" class="form-label fw-bold">
                            {{ form.score.label }} <span class="text-danger">*</span>
                        </label>
                        {% if form.score.errors %}
                            <div class="text-danger mb-1 small">{{ form.score.errors.0 }}</div>
                        {% endif %}
                        <div class="input-group">
                            {{ form.score }}
                            <span class="input-group-text">分</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <label for="{{ form.exam_duration.id_for_label }}" class="form-label fw-bold">
                            {{ form.exam_duration.label }}
                        </label>
                        {% if form.exam_duration.errors %}
                            <div class="text-danger mb-1 small">{{ form.exam_duration.errors.0 }}</div>
                        {% endif %}
                        <div class="input-group">
                            {{ form.exam_duration }}
                            <span class="input-group-text">分钟</span>
                        </div>
                        <div class="form-text mt-1">可选：不填写则保留原用时或显示为空</div>
                    </div>
                </div>

                <!-- 全局错误提示 -->
                {% if form.non_field_errors %}
                    <div class="alert alert-danger mb-4 small">
                        {{ form.non_field_errors.0 }}
                    </div>
                {% endif %}

                <!-- 提交/取消按钮 -->
                <div class="d-flex gap-3 mt-5">
                    <button type="submit" class="btn btn-primary flex-grow-1">
                        <i class="fas fa-save me-1"></i>保存修改
                    </button>
                    <a href="{% url 'score:score_detail' pk=exam_record.pk %}" class="btn btn-outline-danger">
                        <i class="fas fa-times me-1"></i>取消
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}